@echo off

:parameters
rem Descriptive script name
set NAME=Esponce QR Code API 3.0

set MSBUILD="C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe"

set SOLUTION=esponce.sln
rem CWD - Current working directory, should be the solution directory when publishing, but can be other when deploying
set CWD=%CD%
rem CPU_CORE_COUNT - Number of CPU cores for faster builds
set CPU_CORE_COUNT=4

:welcome
echo %NAME%
echo.

:validation
if not "%1"=="" goto %1
if %CWD%=="" goto error-cwd-missing
if not exist %MSBUILD% goto error-msbuild-missing
if exist %SOLUTION% goto default
goto error-solution-missing

:clean
if not exist %SOLUTION% goto error-solution-missing
echo Cleaning...
for /F %%i in ('dir /s /b *.csproj') do (
	echo Cleaning %%i
	%MSBUILD% %%i /t:Clean /p:WarningLevel=1;Configuration=Debug /v:m /m:%CPU_CORE_COUNT% /nologo
	%MSBUILD% %%i /t:Clean /p:WarningLevel=1;Configuration=Release /v:m /m:%CPU_CORE_COUNT% /nologo
)
goto end

:build-all
if not exist %SOLUTION% goto error-solution-missing
echo Building...
for /F %%i in ('dir /s /b *.csproj') do (
	echo Building %%i
	%MSBUILD% %%i /t:Build /p:WarningLevel=0;Configuration=Debug /v:q /m:%CPU_CORE_COUNT% /nologo
	%MSBUILD% %%i /t:Build /p:WarningLevel=0;Configuration=Release /v:q /m:%CPU_CORE_COUNT% /nologo
)
goto end

:unbind
rem Remove all files for SourceSafe reference
echo Removing all SourceSafe files...
del /S /F /Q *.vssscc
del /S /F /Q *.vspscc
del /S /F /Q *.scc
del /S /F /Q *.suo
del /S /F /Q *.user
del /S /F /Q /A:H *.scc
del /S /F /Q /A:H *.suo
goto end

:default
:build
if not exist %SOLUTION% goto error-solution-missing
echo --------------------------------------------------------------------------------
echo Building...

rem Build .NET 3.5 library
set OUT_DIR=bin\net35
if exist "%OUT_DIR%" rmdir /S /Q "%OUT_DIR%"
%MSBUILD% api\net35\QRCode.csproj /t:Rebuild /p:OutDir=..\..\%OUT_DIR%\ /p:WarningLevel=1;Configuration=Release /v:m /m:%CPU_CORE_COUNT% /nologo

set OUT_DIR=bin\silverlight
if exist "%OUT_DIR%" rmdir /S /Q "%OUT_DIR%"
%MSBUILD% api\silverlight\QRCode.Silverlight.csproj /t:Rebuild /p:OutDir=..\..\%OUT_DIR%\ /p:WarningLevel=1;Configuration=Release /v:m /m:%CPU_CORE_COUNT% /nologo

set OUT_DIR=bin\wp7
if exist "%OUT_DIR%" rmdir /S /Q "%OUT_DIR%"
%MSBUILD% api\wp7\QRCode.WP7.csproj /t:Rebuild /p:OutDir=..\..\%OUT_DIR%\ /p:WarningLevel=1;Configuration=Release /v:m /m:%CPU_CORE_COUNT% /nologo

rem /t:<project>:<target>
rem /p:<property1>;<property2>;WarningLevel=1;Configuration=%CONFIG%;OutputDir=bin\Debug
rem /v:<verbosity>     - q[uiet], m[inimal], n[ormal], d[etailed], diag[nostic]
rem /m:2               - number of msbuild.exe processes, 2 for dual core
rem /fl                - file logger, create msbuild.log
rem /nologo            - hides Microsoft (c) ... message
rem /fileLoggerParameters:LogFile=MyLog.log;Append;Verbosity=diagnostic;Encoding=UTF-8
rem /noconsolelogger   - if running from background script

echo.
echo Completed!

goto end

:help
:-help
:--help
:-?
:/?
:/help
:usage
echo Usage:
echo   make [ help ^| clean ^| build ]
echo.
echo Options:
echo   help      display this message
echo   clean     remove all bin\* and obj\* files
echo   build     build all projects in solution
goto end

:error-solution-missing
echo Error: Cannot find %SOLUTION%
echo Search location: %CD%
echo Tip: copy this script to the solution directory
goto end

:error-msbuild-missing
echo Error: Cannot find MSBuild.exe
echo Search location: %MSBUILD%
goto end

:error-cwd-missing
echo Error: cannot get current working directory
goto end

:end-error
echo Error!
goto end

:end